﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using G.Net;
using System.ComponentModel;

namespace G.Reader.Utils
{
    public class FileUtil
    {
        private static System.Threading.AutoResetEvent autoReset = new System.Threading.AutoResetEvent(false);
        int bytesRead;
        bool isFisrt = true;
        byte[] uploadBuffer = new byte[51200];//一次上传 50KB
        System.IO.Stream stream;
        string fileName;
        public event ProgressChangedEventHandler DownloadProgressChanged;
        public event EventHandler DownloadComplete;

        #region 上传
        public event ProgressChangedEventHandler UploadProgressChanged;
        public event EventHandler UploadComplete;
        public void ChangeEncoding(System.IO.Stream orignal, string filename)
        {
            stream = new System.IO.MemoryStream();
            fileName = filename;
            orignal.CopyTo(stream);
            stream.Seek(0, System.IO.SeekOrigin.Begin);
            Upload(null);
            isFisrt = false;
        }
        private void Upload(string s)
        {
            if (!isFisrt)
            {
                if (UploadProgressChanged != null)
                {
                    UploadProgressChanged(this, new ProgressChangedEventArgs((int)(stream.Position * 100 / stream.Length), null));
                }
            }
            bytesRead = stream.Read(uploadBuffer, 0, uploadBuffer.Length);
            if (bytesRead != 0)
            {
                UploadData(Upload);
            }
            else
            {
                if (UploadComplete != null)
                    UploadComplete(this, EventArgs.Empty);
            }
        }
        private void UploadData(Action<string> callback)
        {
            HttpWebRequest request = HttpWebRequest.Create("http://180.168.153.162:4530/BookUtil.ashx") as HttpWebRequest;
            request.Headers["method"] = "upload";
            request.Headers["filetype"] = "book";
            request.Headers["filename"] = HttpUtility.UrlEncode(fileName);
            request.Headers["isfirst"] = isFisrt.ToString();
            request.Headers["userid"] = GloabalData.Instance.User.Id.ToString();
            request.Method = "POST";
            request.BeginGetRequestStream((i1) =>
            {
                var stream = request.EndGetRequestStream(i1);
                stream.Write(uploadBuffer, 0, bytesRead);
                stream.Flush();
                stream.Close();
                request.BeginGetResponse((i2) =>
                {
                    var response = request.EndGetResponse(i2);
                    using (var reader = new System.IO.StreamReader(response.GetResponseStream()))
                    {
                        var str = reader.ReadToEnd();
                        callback(str);
                    }
                }, null);
            }, null);
        }
        #endregion
    }
}
